public class Day10 {
    // 1047、删除字符串中所有相邻的重复项
    public String removeDuplicates(String s) {
        // 使用栈的思想
        StringBuffer ret = new StringBuffer();
        char[] ss = s.toCharArray();
        // 每次放入ret之前和最顶上的字符进行比较，如果相等就不入并且把顶上字符取出
        for(char ch : ss) {
            if(ret.length() > 0 && ch == ret.charAt(ret.length() - 1)) {
                ret.deleteCharAt(ret.length() - 1);
            }else {
                ret.append(ch);
            }
        }

        return ret.toString();
    }

    // 844、比较含退格的字符串
    public boolean backspaceCompare(String s, String t) {
        return changeStr(s).equals(changeStr(t));
    }

    public String changeStr(String str) {
        StringBuilder ret = new StringBuilder();
        char[] strs = str.toCharArray();
        int i = 0,n = str.length();
        while(i < n) {
            if(strs[i] != '#') {
                ret.append(strs[i]);
            }else {
                if(ret.length() > 0) {
                    ret.deleteCharAt(ret.length() - 1);
                }
            }
            i++;
        }
        return ret.toString();
    }
}
